//
//  LoginViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/18.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistFViewController.h"
#import <IDLFaceSDK/FaceSDKManager.h>
#import "LivenessViewController.h"
#import "LivingConfigModel.h"
#import "DetectionViewController.h"
#import "HostLoginViewController.h"
#import "AppDelegate.h"
#import "ChangeRoleViewController.h"
#import "BloginViewController.h"
#import "RuleViewController.h"
#import "ResetPassViewController.h"
@interface LoginViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@end

@implementation LoginViewController
- (IBAction)face_login:(id)sender {
    if ([[FaceSDKManager sharedInstance] canWork]) {
        NSString* licensePath = [[NSBundle mainBundle] pathForResource:FACE_LICENSE_NAME ofType:FACE_LICENSE_SUFFIX];
        [[FaceSDKManager sharedInstance] setLicenseID:FACE_LICENSE_ID andLocalLicenceFile:licensePath];
    }
//    DetectionViewController* dvc = [[DetectionViewController alloc] init];
//    dvc.completion=^(NSDictionary *images, UIImage *originImage){
//        NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//    NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//        //self.base_Str = base_Str;
//       [self uploadFace:base_Str];
//        NSLog(@"base-------%@",base_Str);
//       // UIImage* bestImage = [UIImage imageWithData:data];
//    };
    
    //活体检测
    LivenessViewController* lvc = [[LivenessViewController alloc] init];
    LivingConfigModel* model = [LivingConfigModel sharedInstance];
    NSArray *arr = @[@(FaceLivenessActionTypeLiveEye)];
    model.isByOrder = YES;
    [model.liveActionArray addObjectsFromArray:arr];
    [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
    lvc.completion=^(NSDictionary *images, UIImage *originImage){
        if([images[@"bestImage"] count] > 0){
            NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
            NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            //self.base_Str = base_Str;
            [self uploadFace:base_Str];
            NSLog(@"base-------%@",base_Str);
        }
    };
    [self pushSecondVC:lvc];
    
}


//忘记密码
- (IBAction)forgtPassword:(UIButton *)sender {
    ResetPassViewController *vc = [[ResetPassViewController alloc]init];
    vc.isfind = 1;
    [self pushSecondVC:vc];
}
-(void)setAttText{
    NSString *price_text = @"点击登录即代表您同意";
    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Light" size: 13],
                                NSForegroundColorAttributeName:[UIColor grayColor] };
    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [price_text substringWithRange: NSMakeRange(0, 10)] attributes: attrDict1];
    NSString *price_text1 = @"《板扎秀秀直播》相关协议";
    NSDictionary *attrDict = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Light" size: 13],
                                NSForegroundColorAttributeName:RGB(47, 174, 255) };
    NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [price_text1 substringWithRange: NSMakeRange(0, price_text1.length)] attributes: attrDict];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithAttributedString:attrStr1];
    [str appendAttributedString:attrStr2];
    [self.agren_Btn setAttributedTitle:str forState:UIControlStateNormal];
}
- (IBAction)showPass:(UIButton *)sender {
    self.pass_Tf.secureTextEntry = 0;
}
-(void)uploadFace:(NSString *)base64{
    NSDictionary *param = @{@"image":base64,@"ta":@"face_login",@"op":@"login",@"ctrl":@"live",@"ac":@"auth"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            
            [XTool SaveDefaultInfo:response[@"data"] Key:USER_INFO];
            [XTool SaveDefaultInfo:@"0" Key:Prole];
            NSLog(@"re===%@",response[@"data"]);
            [WToast showWithText:@"登录成功"];
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app changeRoot:0];

        }
    } failure:^(NSError *error) {
        
    }];
}
- (IBAction)login:(UIButton *)sender {
    [self.view endEditing:1];
    if([self.mob_Tf.text isEqualToString:@""]||[self.pass_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请填写完整的登录信息"];
        return;
    }
    NSDictionary *params = @{@"ctrl":@"live",@"ac":@"auth",@"op":@"login",@"mobile":self.mob_Tf.text,@"password":self.pass_Tf.text};
    [self weqPostHandle:params success:^(id response) {
        if(response){
            
            [XTool SaveDefaultInfo:[NSDictionary changeType:response[@"data"]] Key:USER_INFO];
             [XTool SaveDefaultInfo:@"0" Key:Prole];
            
            [WToast showWithText:@"登录成功"];
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app changeRoot:0];
          
            
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self setNavigation:1 andTitle:@"" andNaviColor:nil andTitleColor:nil];
     [self setNavigation:1 andTitle:@"" andNaviColor:nil andTitleColor:nil andRightimg:@"设置个人资料551" andsel:@selector(set)];
    self.botom_ScroView.contentSize = CGSizeMake(screen_width, screen_height*1.5);
    self.botom_ScroView.delegate = self;
    self.botom_ScroView.showsVerticalScrollIndicator = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKb)];
    [self.view addGestureRecognizer:tap];
    self.mob_Tf.delegate = self;
    self.pass_Tf.delegate = self;
    self.face_Btn.backgroundColor = BUTTON_COLOR;
    [self setAttText];
    // Do any additional setup after loading the view from its nib.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if(textField==self.pass_Tf){
        if(![self.mob_Tf.text isEqualToString:@""]){
        self.login_Btn.backgroundColor = BUTTON_COLOR;
        }
    }
    if(textField==self.mob_Tf){
        if(![self.pass_Tf.text isEqualToString:@""]){
            self.login_Btn.backgroundColor = BUTTON_COLOR;
        }
    }
}

-(void)set{
    ChangeRoleViewController *vc = [[ChangeRoleViewController alloc]init];
    [self pushSecondVC:vc];
}
-(void)hideKb{
    [self.view endEditing:1];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:1];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)gotoregist:(UIButton *)sender {
    RegistFViewController *vc = [[RegistFViewController alloc]init];
    [self pushSecondVC:vc];
}
- (IBAction)hostLogin:(UIButton *)sender {
//    HostLoginViewController *vc = [[HostLoginViewController alloc]init];
//    [self pushSecondVC:vc];
}
- (IBAction)host_login:(UIButton *)sender {
    HostLoginViewController *vc = [[HostLoginViewController alloc]init];
    [self pushSecondVC:vc];
}

- (IBAction)check_Rule:(id)sender {
    RuleViewController *vc = [[RuleViewController alloc]init];
    [self pushSecondVC:vc];
}
- (IBAction)bussionLogin:(UIButton *)sender {
//    [WToast showWithText:@"该端口暂未开放"];
    BloginViewController *vc = [[BloginViewController alloc]init];
    [self pushSecondVC:vc];
}
@end
